var express = require('express')
var router = express.Router()
var comments = require('./../models/comment')
let moment = require('moment')
// 提交评论
router.post('/postComments', function (req, res, next) {
	let { articleId, tourist, autor, headPic, context } = req.body
	let commentObj = req.body //评论数据

	let obj = {
		id: 1, //评论id
		tourist: tourist, //是否是游客  1是，0不是
		autor: autor, //用户名
		headPic:
			'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=3156590071,3197389731&fm=26&gp=0.jpg', //头像
		context: context, //内容
		tops: 0, //是否置顶，置顶1，不置顶0
		hiden: 1, //是否隐藏，隐藏1，不隐藏0

		times: moment(new Date()).format('YYYY-MM-DD HH:mm:ss'), //时间

		Reply: [],
	}
	commentObj.articleId = articleId
	commentObj.list = [obj]
	comments.findOne({ articleId }, function (err0, doc0) {
		/*逻辑【1】 新建集合
		 **  整理数据
		 **  第一个评论设置id为1
		 */
		if (doc0 == '' || doc0 == null) {
			// 插入数据
			comments.create(commentObj, function (err1, doc1) {
				if (err1) {
					res.json({
						code: '201',
						data: null,
						message: err1,
						success: true,
					})
				} else {
					res.json({
						code: '200',
						data: null,
						message: '成功',
						success: true,
					})
				}
			})
		} else {
			obj.id = doc0.list[doc0.list.length - 1].id + 1
			doc0.list.push(obj)

			doc0.save(function (err2, doc2) {
				if (err2) {
					res.json({
						code: '201',
						data: null,
						message: err2,
						success: true,
					})
				} else {
					res.json({
						code: '200',
						data: null,
						message: '成功',
						success: true,
					})
				}
			})
		}
	})
})

// 提交回复
router.post('/postRelay', function (req, res, next) {
	let { articleId, commentId, tourist, autor, headPic, context } = req.body

	let obj = {
		id: 1, //回复id
		tourist: tourist, //是否是游客  1是，0不是
		autor: autor, //用户名
		headPic:headPic, //头像
		context: context, //内容
		tops: 0, //是否置顶，置顶1，不置顶0
		hiden: 1, //是否隐藏，隐藏1，不隐藏0

		times: moment(new Date()).format('YYYY-MM-DD HH:mm:ss'), //时间
	}
	comments.findOne({ articleId }, function (err0, doc0) {
		/*逻辑【1】 新建回复
		 **  整理数据
		 **  第一个回复设置id为1
		 */
		doc0.list.forEach((item, index) => {
			if (item.id == commentId) {
				if (item.Reply.length == 0) {
					// 回复未空，初始化插入回复
					doc0.list[index].Reply.push(obj)
				} else {
					// 已有回复，叠加回复，处理id，递增1
					let replay = doc0.list[index].Reply
					obj.id = replay[replay.length - 1].id + 1
					doc0.list[index].Reply.push(obj)
				}
			}
		})

		doc0.save(function (err1, doc1) {
			if (err1) {
				res.json({
					code: '201',
					data: null,
					message: err1,
					success: true,
				})
			} else {
				res.json({
					code: '200',
					data: null,
					message: '成功',
					success: true,
				})
			}
		})
	})
})

// 删除文章
router.post('/delete', function (req, res, next) {
	var param = {
		userName: req.body.data.userName,
	}
	let id = req.body.data.id
	let types = req.body.data.types
	articles.findOne(param, function (err, doc) {
		// console.log(doc)
		doc.article[types].forEach((item, index) => {
			if (item.id == id) {
				doc.article[types].splice(index, 1)
				// console.log(doc.article.baidu[0].id)
				doc.save(function (err1, doc1) {
					if (err1) {
						res.json({
							status: '1',
							msg: '删除失败' + err1,
						})
					} else {
						res.json({
							status: '0',
							msg: '删除成功',
						})
					}
				})
			}
		})
	})
})

// 修改评论
router.post('/commentEdit', function (req, res, next) {
	let ids = req.body.data.list.ids
	// let types = req.body.data.types
	// let comment_id = req.body.data.comment_id//评论id
	let list = req.body.data.list //评论内容
	// let Reply_id = req.body.data.Reply_id//回复id
	comments.findOne(function (err, doc) {
		// console.log(doc)
		doc.comment.forEach((item, index) => {
			if (item.ids == ids) {
				//找到文章
				item.list.forEach((item1, index1) => {
					if (item1._id == list._id) {
						//找到评论
						// item1=list  直接这样赋值，会发现数据库没有更改
						item1.show = list.show
						item1.tops = list.tops
						item1.Reply = list.Reply

						doc.save(function (err1, doc1) {
							if (err1) {
								res.json({
									status: '1',
									msg: '修改失败' + err1,
								})
							} else {
								res.json({
									status: '0',
									msg: '修改成功',
								})
							}
						})
					}
				})
			}
		})
	})
})

// 读取评论
router.post('/commentList', function (req, res, next) {
	let { id } = req.body
	comments.findOne({ articleId: id }, function (err0, doc0) {
		if (doc0 == '' || doc0 == null) {
			res.json({
				code: '201',
				data: null,
				message: '评论为空',
				code:500,
			})
		} else {
			res.json({
				code: '200',
				data: doc0.list || [],
				message: '成功',
				success: true,
			})
		}
	})
})

module.exports = router
